'use strict';
// 查询趣事分享列表
const db = uniCloud.database();
exports.main = async (event, context) => {

	// 查询条件
	let parmse = {};
	
	let res = null;

	if (event.state == 'add') {
		parmse = event
		delete parmse['state'] //删除这个多余的状态
		res = await db.collection('amusingList').add(parmse);
		return {
			code: 200,
			message: "操作成功"
		}
	} else if (event.state == 'update') {
		
	} else if (event.state == 'look') {
		parmse = event
		delete parmse['state']
		
		if(parmse.createTime){
			parmse.createTime = new RegExp(parmse.createTime, 'g')
		}else{
			delete parmse['createTime']
		}
		
		res = await db.collection('amusingList').where(db.command.or({
			...parmse
		})).orderBy('createTime', 'desc').get() //获取数据
		console.log(res);
		
		// 只取前十条
		let arr = []
		
		res.data.forEach((item,index)=>{
			if(index <= 10){
				arr.push(item)
			}
		})
		
		return {
			code: 200,
			message: "操作成功",
			data: arr,
			total:res.affectedDocs
		}
	}

}